//
//  CNewOrderPartsCell.m
//  ProjectforHDX
//
//  Created by motom on 16/9/8.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "CNewOrderPartsCell.h"
#import "FailurePartsModel.h"

@interface CNewOrderPartsCell()

@property (nonatomic, weak) UILabel *partNameLB;
@property (nonatomic, weak) RZCustomLable *partName;

@property (nonatomic, weak) UILabel *partCodeLB;
@property (nonatomic, weak) RZCustomLable *partCode;

@property (nonatomic, weak) RZCustomLable *serviceStar;

@property (nonatomic, weak) UILabel *plateSnLB;
@property (nonatomic, weak) RZCustomLable *plateSn;

@property (nonatomic, weak) UILabel *partVersionLB;
@property (nonatomic, weak) RZCustomLable *partVersion;

@property(nonatomic,weak)RZCustomLable *serStar;

@property (nonatomic, weak) UILabel *typeLB;


@property(nonatomic , strong)NSMutableArray *starts;


@end

@implementation CNewOrderPartsCell

-(NSMutableArray *)starts
{
    if (!_starts) {
        _starts = [NSMutableArray array];
    }
    return _starts;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

    }
    return self;
}



#pragma mark 初始化界面
-(void)setupView
{
    //故障件名称lable
    UILabel *partNameLB = [[UILabel alloc] init];
    self.partNameLB = partNameLB;
    partNameLB.text = [NSString stringWithFormat:@"%@:",[UtilCheck language:@"show.panelName"]];
    partNameLB.font = CustomFont(14);
    [partNameLB sizeToFit];
    [self addSubview:partNameLB];
    
    //板件号lable
    UILabel *partCodeLB = [[UILabel alloc]init];
    self.partCodeLB = partCodeLB;
    partCodeLB.text = [NSString stringWithFormat:@"%@:",[UtilCheck language:@"order.part.code"]];
    partCodeLB.font = CustomFont(14);
    [partCodeLB sizeToFit];
    [self addSubview:partCodeLB];
    
    //序列号label
    UILabel *partSnLB = [[UILabel alloc]init];
    self.plateSnLB = partSnLB;
    partSnLB.text = [NSString stringWithFormat:@"%@:",[UtilCheck language:@"setting.plateSerial"]];
    partSnLB.font = CustomFont(14);
    [partSnLB sizeToFit];
    [self addSubview:partSnLB];
    
    //类型label
//    UILabel *typeLB = [[UILabel alloc]init];
//    typeLB.textColor = [UtilCheck hexStringToColor:@"1a57be" alpha:1.0];
//    self.typeLB = typeLB;
//    [self addSubview:typeLB];
    
    
    //故障件名
    RZCustomLable *partName = [[RZCustomLable alloc]init];
    [partName setTextColor:[UtilCheck hexStringToColor:@"868686" alpha:1.0]];
    partName.font = CustomFont(14);
    self.partName = partName;
    self.partName.numberOfLines = 0;
    [self addSubview:partName];
    
    //故障件号
    RZCustomLable *partCode = [[RZCustomLable alloc]init];
    [partCode setTextColor:[UtilCheck hexStringToColor:@"868686" alpha:1.0]];
    partCode.font = CustomFont(14);
    self.partCode = partCode;
    [self addSubview:partCode];
    
    
    if(self.isDefaultPart == YES  )
    {
        //服务级别
        RZCustomLable *serviceStar = [[RZCustomLable alloc]init];
//        [serviceStar setTextColor:[UtilCheck hexStringToColor:@"868686" alpha:1.0]];
        serviceStar.font = CustomFont(14);
        self.serviceStar = serviceStar;
        [self addSubview:serviceStar];
        
        //服务星级
        RZCustomLable *serStar = [[RZCustomLable alloc]init];
        [serStar setTextColor:[UtilCheck hexStringToColor:@"868686" alpha:1.0]];
        serStar.font = CustomFont(14);
        self.serStar = serStar;
        [self addSubview:serStar];
    }
    NSLog(@"%ld",(long)self.index);
    if (self.isDefaultPart == NO || self.index == 1) {
        RZCustomLable *plateSn = [[RZCustomLable alloc]init];
        [plateSn setTextColor:[UtilCheck hexStringToColor:@"868686" alpha:1.0]];
        plateSn.font = CustomFont(14);
        self.plateSn = plateSn;
        [self addSubview:plateSn];
        
        //版本号lable
            UILabel *partVersionLB = [[UILabel alloc]init];
            self.partVersionLB = partVersionLB;
            partVersionLB.text = [NSString stringWithFormat:@"%@:",[UtilCheck language:@"order.part.version"]];
            partVersionLB.font = CustomFont(14);
            [partVersionLB sizeToFit];
            [self addSubview:partVersionLB];
        
        
        //版本号
            RZCustomLable *partVersion = [[RZCustomLable alloc]init];
            [partVersion setTextColor:[UtilCheck hexStringToColor:@"868686" alpha:1.0]];
            partVersion.font = CustomFont(14);
            self.partVersion = partVersion;
            [self addSubview:partVersion];
        

        
    }
    
}

#pragma mark 添加数据模型
-(void)setDefaultPart:(FailurePartsModel *)part
{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self setupView];
    if (self.starts) {
        [self.starts removeAllObjects];
    }
    self.partName.text = [NSString stringWithFormat:@"%@",part.name];
    [self.partName sizeToFit];
    
    self.partCode.text = [NSString stringWithFormat:@"%@",part.code];
    [self.partCode sizeToFit];

    NSLog(@"%@",part.version);
     self.partVersion.text = [NSString stringWithFormat:@"%@",part.version];
    [self.partVersion sizeToFit];

    //故障件
    if (self.isDefaultPart == YES) {
        if (self.index == 0) {//备件账号
            self.serviceStar.text =[NSString stringWithFormat:@"%@:",[UtilCheck language:@"order.part.serviceStar"]];
            [self.serviceStar sizeToFit];
            
            self.serStar.text = part.rating;
            [self.serStar sizeToFit];
            
//            int i = 0;
//            for (i =0; i<[part.rating integerValue]; i++) {
//                UIImageView *image = [[UIImageView alloc]initWithImage:BDFILEIMAGENAEM(@"Order/img_xin")];
//                [self addSubview:image];
//                [self.starts  addObject:image];
//                [image sizeToFit];
//            }
//            while (i < 5) {
//                UIImageView *image = [[UIImageView alloc]initWithImage:BDFILEIMAGENAEM(@"Order/img_star")];
//                [self addSubview:image];
//                [self.starts  addObject:image];
//                [image setSize:CGSizeMake(12, 12)];
//                i++;
//            }
        }
        if ( [part.faultPartType intValue] == 1) {
            
            NSAttributedString *str = [[NSAttributedString alloc]initWithString: [UtilCheck language:@"customer.searchplate.plate"] attributes:@{NSForegroundColorAttributeName:[UtilCheck hexStringToColor:@"1a57be" alpha:1.0]}];
           
            NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:[ NSString stringWithFormat:@"%@:",[UtilCheck language:@"seeting.type"]]];
            
            [str2 appendAttributedString:str];
            
            [str2 setAttributes:@{NSForegroundColorAttributeName : [UtilCheck hexStringToColor:@"868686" alpha:1.0]} range:NSMakeRange(0, 3)];
            
            self.typeLB.attributedText = str2;
        }
        else {
            
            NSAttributedString *str = [[NSAttributedString alloc]initWithString: [UtilCheck language:@"customer.searchplate.Consumables"] attributes:@{NSForegroundColorAttributeName:[UtilCheck hexStringToColor:@"1a57be" alpha:1.0]}];
           
            NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:[ NSString stringWithFormat:@"%@:",[UtilCheck language:@"seeting.type"]]];
            [str2 appendAttributedString:str];
            [str2 setAttributes:@{NSForegroundColorAttributeName : [UtilCheck hexStringToColor:@"868686" alpha:1.0]} range:NSMakeRange(0, 3)];
            [str2 appendAttributedString:str];

            self.typeLB.attributedText = str2;
        }
        
        [self.typeLB sizeToFit];
    }
    else if(self.isDefaultPart == NO) {
        
        self.plateSn.text = part.sn;
        [self.plateSn sizeToFit];
    }
    
    [self layoutSubviews];
}

#pragma mark 自动布局
#define mar 12
-(void)layoutSubviews
{
    
    self.partNameLB.left = 10;
    self.partNameLB.top = 15;
    
    self.partName.left = self.partNameLB.right;
    self.partName.top = 15;
    
    self.partCodeLB.left = 10;
    self.partCodeLB.top = self.partNameLB.bottom + 5;
    
    self.partCode.left = self.partCodeLB.right;
    self.partCode.top = self.partCodeLB.top+1;

    self.partVersionLB.left = 10;
    self.partVersionLB.top = self.partCodeLB.bottom+5;
    
    self.partVersion.left = self.partVersionLB.right;
    self.partVersion.top = self.partVersionLB.top;
    
    if (self.isDefaultPart == YES) {
    self.plateSnLB.hidden =YES;
        
    self.serviceStar.left = 10;
    self.serviceStar.top = self.partCodeLB.bottom+5;

        self.serStar.left = self.serviceStar.right;
        self.serStar.top = self.serviceStar.top;
        
        if (self.index == 0) {
            [self addSoldLine:1.0 soldcolor:[UtilCheck hexStringToColor:@"F7F5FA" alpha:1.0] pointA:CGPointMake(0, 85) pointB:CGPointMake([UIScreen mainScreen].bounds.size.width, 85)];
            self.typeLB.font = CustomFont(14);
            self.typeLB.top = self.serviceStar.bottom + 3;
            self.typeLB.right = self.serviceStar.right + 75;
        }
        else{
            [self addSoldLine:0.5 soldcolor:[UtilCheck hexStringToColor:@"F7F5FA" alpha:1.0] pointA:CGPointMake(0, 85) pointB:CGPointMake([UIScreen mainScreen].bounds.size.width, 85)];
        }
            self.typeLB.font = CustomFont(14);
            self.typeLB.top = self.partVersionLB.bottom + 3;
            self.typeLB.left = 10;
    }
    else if(self.isDefaultPart== NO)
    {
        
        self.partNameLB.top = 15;
        self.partNameLB.left = 10;
        
        self.partName.top = self.partNameLB.top;
        self.partName.left = self.partNameLB.right;
        
        self.partCodeLB.top = self.partNameLB.bottom + 5;
        self.partCodeLB.left = 10;
        
        self.partCode.left = self.partCodeLB.right;
        self.partCode.top = self.partCodeLB.top;
        
        self.plateSnLB.top = self.partVersionLB.bottom +5 ;
        self.plateSnLB.left = 10;
        
        self.plateSn.left = self.plateSnLB.right;
        self.plateSn.top = self.plateSnLB.top;
        

        [self addSoldLine:1.0 soldcolor:[UtilCheck hexStringToColor:@"F7F5FA" alpha:1.0] pointA:CGPointMake(0, 105) pointB:CGPointMake([UIScreen mainScreen].bounds.size.width, 105)];
    }
    
    self.cellHeight = self.partName.bottom+10;
    
}
@end
